package com.ysm.smfu.wxapi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.ysm.smfu.MainActivity;
import com.ysm.smfu.activity.FoundActivity;
import com.ysm.smfu.activity.XiangQingActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * Created by Administrator on 2016/12/9.
 */
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private IWXAPI api;
    private SharedPreferences currShared;
    private boolean isOnline;
    private String code;
    private String type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, SpKey.WXAPPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
//                支付成功，将订单信息和支付信息清空
                String name = new SharedConfig(UIUtils.getContext()).getCommoneSp().getString(SpKey.NAME, "");
                currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
                currShared.edit().putString(SpKey.Pay_BUSSINESSID, "").commit();
                currShared.edit().putString(SpKey.JIFEN, "").commit();
                currShared.edit().putString(SpKey.PAY_PRICE, "").commit();
                currShared.edit().putString(SpKey.REMARK, "").commit();
//                web支付成功不展示成功界面
                isOnline = currShared.getBoolean("isOnline", false);
                type = currShared.getString("type", "");
                code = currShared.getString("code", "");
                if (!isOnline) {
                    String payInfo_price = currShared.getString("payInfo_price", "");
                    String payInfo_points = currShared.getString("payInfo_points", "");
                    String payInfo_returnPoints = currShared.getString("payInfo_returnPoints", "");
                    String payInfo_name = currShared.getString("payInfo_name", "");
                    String payInfo_headPhoto = currShared.getString("payInfo_headPhoto", "");
                    com.ysm.smfu.bean.PayResult payResult = new com.ysm.smfu.bean.PayResult();
                    payResult.price = payInfo_price;
                    payResult.points = payInfo_points;
                    payResult.name = payInfo_name;
                    payResult.headPhone = payInfo_headPhoto;
                    payResult.returnPoints = payInfo_returnPoints;
                    Intent intent = new Intent(this, XiangQingActivity.class);
                    intent.putExtra("xiangqing", 1);
                    intent.putExtra(SpKey.PAYRESULT, payResult);
//                    SysUtil.printRespones("支付结果：" + payResult);
                    startActivity(intent);
                } else {
                    goSucessPage(code);
                }
            }
        }
        //将需要回显的数据清空
        if (currShared != null) {
            currShared.edit().putString("payInfo_price", "").commit();
            currShared.edit().putString("payInfo_points", "").commit();
            currShared.edit().putString("payInfo_name", "").commit();
            currShared.edit().putString("payInfo_headPhoto", "").commit();
            currShared.edit().putString("payInfo_returnPoints", "").commit();
        }
        finish();
    }

    /**
     * 跳往成功界面
     * /wap/ordersuccess
     *
     * @param code
     */
    private void goSucessPage(String code) {
        if (TextUtils.equals(type, "wechat://"))
            goWxchatPay(code);
        else if (TextUtils.equals(type, "wechatvipup://"))
            goWxChatVip();
        else if (TextUtils.equals(type, "wechatrecharge://"))
            goWxChatCharge();
        else if (TextUtils.equals(type, "wechatactivity://"))
            goWxActivity();
//        goWxchatPay(code);
        clearWxPayInfo();
    }

    /**
     * 微信活动支付成功
     */
    private void goWxActivity() {
//        UIUtils.startActivity(FoundActivity.class, "found", 15);
        Intent intent = new Intent(this, FoundActivity.class);
        intent.putExtra("found", 15);
        intent.putExtra("code", code);
        startActivity(intent);
    }

    /**
     * 微信充值成功界面
     */
    private void goWxChatCharge() {
        UIUtils.startActivity(FoundActivity.class, "found", 14);
    }

    /**
     * 微信升级VIp，成功界面
     */
    private void goWxChatVip() {
        UIUtils.startActivity(MainActivity.class, "isVip", "isVip");
    }

    private void goWxchatPay(String code) {
        Intent intent = new Intent(this, FoundActivity.class);
        intent.putExtra("found", 12);
        intent.putExtra("code", code);
        startActivity(intent);
    }

    /**
     * 清空微信支付信息
     */
    private void clearWxPayInfo() {
        currShared.edit().putString("code", "").commit();
        currShared.edit().putString("type", "").commit();
        currShared.edit().putBoolean("isOnline", false).commit();
    }
}
